package main

import (
	"fmt"
	"log"
	"os"

	"golang.org/x/sys/windows/registry"
)

var logger *log.Logger

func init() {
	logFile, err := os.OpenFile("installLog.txt", os.O_RDWR|os.O_CREATE, 0777)
	if err != nil {
		fmt.Printf("open file error=%s\r\n", err.Error())
		os.Exit(-1)
	}

	// defer logFile.Close()
	//logger:=log.New(logFile,"\r\n", log.Ldate | log.Ltime | log.Llongfile)
	logger = log.New(logFile, "\r\n", log.Ldate|log.Ltime|log.Lshortfile)
}
func regKey(path string) error {
	vconsoleKey, ok, err := registry.CreateKey(registry.CLASSES_ROOT, `ceeloConsole`, registry.CREATE_SUB_KEY|registry.SET_VALUE)
	if err != nil {
		logger.Printf("CreateKey console err :%v\n", err)
		return err
	}
	if ok {
		return err
	}
	defer vconsoleKey.Close()
	vconsoleKey.SetStringValue("URL Protocol", "")
	// 要检测执行文件当前目录
	vconsoleKey.SetStringValue("DefaultIcon", path)
	shellKey, _, err := registry.CreateKey(vconsoleKey, `shell`, registry.CREATE_SUB_KEY)
	if err != nil {
		logger.Printf("CreateKey shell err :%v\n", err)
		return err
	}
	openKey, _, err := registry.CreateKey(shellKey, `open`, registry.CREATE_SUB_KEY)
	if err != nil {
		logger.Printf("CreateKey open err :%v\n", err)
		return err
	}
	commandKey, _, err := registry.CreateKey(openKey, `command`, registry.CREATE_SUB_KEY|registry.SET_VALUE)
	if err != nil {
		logger.Printf("CreateKey err :%v\n", err)
		return err
	}
	commandKey.SetStringValue("", path)

	return nil
}
func cancleReg() {
	err := registry.DeleteKey(registry.CLASSES_ROOT, `ceeloConsole`)
	if err != nil {
		logger.Println("Clean Reg Err:", err)
		return
	}
	logger.Println("clear ok!")
}
func main() {
	cancleReg()
	// path := `C:\ceelo`
	// argNum := len(os.Args)
	// name := path + `\` + `ceeloConsole.exe`
	// if argNum > 1 {
	// 	name = os.Args[1]
	// }
	// logger.Printf("RegKey begin\n")
	// err := regKey(name)
	// if err != nil {
	// 	logger.Printf("Write Reg Err:%v\n", err)
	// 	return
	// }
	// logger.Printf("ReKey Over!\n")
}
